import request from '@/utils/request';

// 通知相关API接口
export const notificationApi = {
  // 获取所有通知（管理员使用）
  getAllNotifications: async (pageNum = 1, pageSize = 10) => {
    try {
      const response = await request({
        url: `/notification/all`,
        method: 'get',
        params: { pageNum, pageSize },
        headers: {
          'Authorization': sessionStorage.getItem('token')
        }
      });
      return response;
    } catch (error) {
      console.error('获取所有通知失败:', error);
      return { code: 500, message: '获取所有通知失败', data: null };
    }
  },
  
  // 获取学生的通知列表
  getStudentNotifications: async (studentId, pageNum = 1, pageSize = 10) => {
    try {
      const response = await request({
        url: `/notification/student/${studentId}`,
        method: 'get',
        params: { pageNum, pageSize },
        headers: {
          'Authorization': sessionStorage.getItem('token')
        }
      });
      return response;
    } catch (error) {
      console.error('获取通知列表失败:', error);
      return { code: 500, message: '获取通知列表失败', data: null };
    }
  },
  
  // 获取教师发送的通知列表
  getTeacherNotifications: async (teacherId, pageNum = 1, pageSize = 10) => {
    try {
      const response = await request({
        url: `/notification/teacher/${teacherId}`,
        method: 'get',
        params: { pageNum, pageSize },
        headers: {
          'Authorization': sessionStorage.getItem('token')
        }
      });
      return response;
    } catch (error) {
      console.error('获取教师发送的通知失败:', error);
      return { code: 500, message: '获取教师发送的通知失败', data: null };
    }
  },
  
  // 获取通知详情
  getNotificationDetail: async (notificationId, studentId) => {
    try {
      const response = await request({
        url: `/notification/${notificationId}`,
        method: 'get',
        params: { studentId },
        headers: {
          'Authorization': sessionStorage.getItem('token')
        }
      });
      return response;
    } catch (error) {
      console.error('获取通知详情失败:', error);
      return { code: 500, message: '获取通知详情失败', data: null };
    }
  },
  
  // 标记通知为已读
  markAsRead: async (notificationId, studentId) => {
    try {
      const response = await request({
        url: `/notification/${notificationId}/read`,
        method: 'put',
        params: { studentId },
        headers: {
          'Authorization': sessionStorage.getItem('token')
        }
      });
      return response;
    } catch (error) {
      console.error('标记通知已读失败:', error);
      return { code: 500, message: '标记通知已读失败', data: null };
    }
  },
  
  // 删除通知（管理员或发送者）
  deleteNotification: async (notificationId) => {
    try {
      const response = await request({
        url: `/notification/${notificationId}`,
        method: 'delete',
        headers: {
          'Authorization': sessionStorage.getItem('token')
        }
      });
      return response;
    } catch (error) {
      console.error('删除通知失败:', error);
      return { code: 500, message: '删除通知失败', data: null };
    }
  },

  // 获取学生未读通知数量
  getUnreadCount: async (studentId) => {
    try {
      const response = await request({
        url: '/notification/unread/count',
        method: 'get',
        params: { studentId },
        headers: {
          'Authorization': sessionStorage.getItem('token')
        }
      });
      return response;
    } catch (error) {
      console.error('获取未读通知数量失败:', error);
      return { code: 500, message: '获取未读通知数量失败', data: null };
    }
  },

  // 发送通知
  sendNotification: async (notificationDto, senderId) => {
    try {
      const response = await request({
        url: '/notification/send',
        method: 'post',
        params: { senderId },
        data: notificationDto,
        headers: {
          'Authorization': sessionStorage.getItem('token')
        }
      });
      return response;
    } catch (error) {
      console.error('发送通知失败:', error);
      return { code: 500, message: '发送通知失败', data: null };
    }
  }
}; 